/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ps.yj.y1j0.data.database.sqlite;

import ps.yj.y1j0.data.YJOrderBy;
import ps.yj.y1j0.data.YJWhere;
import ps.yj.y1j0.data.database.YJSqlManager;
import ps.yj.y1j0.data.database.YJSqlManagerImpl;

/**
 * @author yanjun
 */
public class YJSqliteSqlManager implements YJSqlManager {
    private static YJSqliteSqlManager yjSqliteSqlManager = null;
    private final YJSqlManager sqlManager;

    private YJSqliteSqlManager() {
        sqlManager = YJSqlManagerImpl.getInstance();
    }

    public synchronized static YJSqlManager getInstance() {
        if (yjSqliteSqlManager == null)
            yjSqliteSqlManager = new YJSqliteSqlManager();
        return yjSqliteSqlManager;
    }

    @Override
    public String createInsert(String tableName, String idProperty, String[] properties) {
        return sqlManager.createInsert(tableName, idProperty, properties);
    }

    @Override
    public String createUpdate(String tableName, String idProperty, String[] properties) {
        return sqlManager.createUpdate(tableName, idProperty, properties);
    }

    @Override
    public String createDelete(String tableName, String idProperty, int idCount) {
        return sqlManager.createDelete(tableName, idProperty, idCount);
    }

    @Override
    public String createSelect(String selectSql, YJWhere[] whereArray, YJOrderBy[] orderByArray, String[] groupByArray) {
        return sqlManager.createSelect(selectSql, whereArray, orderByArray, groupByArray);
    }

    @Override
    public String createTable(String tableName, String idProperty, String[] properties) {
        String sql = "CREATE TABLE " + tableName + " (";
        int i = 0;
        for (String property : properties) {
            if (property.equals(idProperty)) continue;
            if (i++ > 0) {
                sql += ",";
            }
            sql += property;
            if (property.startsWith("t_")) {
                sql += " TEXT";
            } else if (property.startsWith("n_")) {
                sql += " NUMERIC";
            } else if (property.startsWith("b_")) {
                sql += " BLOB";
            } else {
                sql += " TEXT";
            }
        }
        sql += ")";
        return sql;
    }

//    @Override
//    public String alertTable(String tableName, String[] properties) {
//        return sqlManager.alertTable(tableName,properties);
//    }

    @Override
    public String dropTable(String tableName) {
        return sqlManager.dropTable(tableName);
    }

    @Override
    public String checkTable(String tableName) {
        String sql = "SELECT COUNT(1) AS num FROM sqlite_master WHERE TYPE = 'table' AND NAME = '" + tableName + "'";
        return sql;
    }
}
